package org.smarthings.things.management.impl.metadata.provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.smarthings.things.management.metadata.ThingActionArgumentMetadata;
import org.smarthings.things.management.metadata.ThingActionMetadata;
import org.smarthings.things.management.metadata.ThingDataType;
import org.smarthings.things.management.metadata.provider.ThingActionMetadataProvider;

@Component(immediate = true)
@Service(ThingActionMetadataProvider.class)
public class DefaultThingActionMetadataProvider implements ThingActionMetadataProvider {

	private Map<String, ThingActionMetadata> actions;

	public DefaultThingActionMetadataProvider() {
		ThingActionArgumentMetadata delay = new ThingActionArgumentMetadata(
				"Delay before the actual toggle operation is performed on the object.", "delay",
				"optional", ThingDataType.INTEGER, "10", "60");
		List<ThingActionArgumentMetadata> onoff = new ArrayList<ThingActionArgumentMetadata>(2);
		onoff.add(delay);
		ThingActionArgumentMetadata level = new ThingActionArgumentMetadata(
				"Exact value of the level to configure a multi-leveled characteristic on the object.", "level",
				"mandatory", ThingDataType.DOUBLE, "0", "100");
		List<ThingActionArgumentMetadata> levelm = new ArrayList<ThingActionArgumentMetadata>(2);
		levelm.add(level);
		actions = new HashMap<String, ThingActionMetadata>(3);
		actions.put("toggle", new ThingActionMetadata("Toggles a binary switch object.", onoff));
		actions.put("on", new ThingActionMetadata("Switches on a binary switch object.", onoff));
		actions.put("off", new ThingActionMetadata("Switches off a binary switch object.",
				onoff));

		actions.put("resetTotal", new ThingActionMetadata(
				"Reset the measurement accumulated by the object for a specific amount of time.",
				null));
		actions.put("resetAverage", new ThingActionMetadata(
				"Reset the average measurement calculated by the object for a specific amount of time.",
				null));
		actions.put("levelUp", new ThingActionMetadata(
				"Configures the level of a multi-leveled characteristic controlled by this object with one unit up",
				null));
		actions.put("levelDown", new ThingActionMetadata(
				"Configures the level of a multi-leveled characteristic on this object with one unit down.",
				null));
		actions.put("configureLevel", new ThingActionMetadata(
				"Configures the level of a multi-leveled characteristic on this object to the exact value given as argument.",
				null));
	}

	@Override
	public synchronized List<String> getThingActions() {
		return new ArrayList<String>(actions.keySet());
	}

	@Override
	public synchronized ThingActionMetadata getThingActionDescription(String action) {
		return actions.get(action);
	}

	@Override
	public synchronized void registerThingActionMetadata(String action, ThingActionMetadata metadata) {
		actions.put(action, metadata);

	}

	@Override
	public synchronized void unregisterThingActionMetadata(String action) {
		actions.remove(action);
	}

}
